﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _050.文件操作_流
{
	internal class Program
	{
		static void Main(string[] args)
		{
			//ByteStream();
			//ByteStream2();

		}

		// 字节流
		static void ByteStream()
		{
			DateTime before = DateTime.Now;

			// 文件复制 FileMode 文件的打开方式  FileAccess 文件的访问权限
			FileStream readFileStream = new FileStream(@"D:\VisualStudioWork\CSharp编程基础语法\README.md", FileMode.Open, FileAccess.Read);
			FileStream writeFileStream = new FileStream(@"D:\VisualStudioWork\CSharp编程基础语法\README2.md", FileMode.Create, FileAccess.Write);

			// 字节流的方式读取

			// 读取一个字节
			//readFileStream.ReadByte();
			// 写入一个字节
			//writeFileStream.WriteByte(0);

			int nextByte = -1;
			while ((nextByte = readFileStream.ReadByte()) != -1)
			{
				writeFileStream.WriteByte((byte)nextByte);
			}
			// 关闭流
			writeFileStream.Close();
			readFileStream.Close();

			DateTime after = DateTime.Now;
			TimeSpan ts = after.Subtract(before);
			Console.WriteLine(ts);
		}

		// 字节流 优化
		static void ByteStream2()
		{
			DateTime before = DateTime.Now;

			// 文件复制 FileMode 文件的打开方式  FileAccess 文件的访问权限
			FileStream readFileStream = new FileStream(@"D:\VisualStudioWork\CSharp编程基础语法\README.md", FileMode.Open, FileAccess.Read);
			FileStream writeFileStream = new FileStream(@"D:\VisualStudioWork\CSharp编程基础语法\README2.md", FileMode.Create, FileAccess.Write);

			// 字节流的方式读取

			byte[] buffer = new byte[1024];
			// count 实际读取的多少个字节
			//int count = readFileStream.Read(buffer, 0, 1024);
			//writeFileStream.Write(buffer, 0, count);
			int count = 0;  // 等于0表示到末尾了
			while ((count = readFileStream.Read(buffer, 0, 1024)) != 0)
			{
				writeFileStream.Write(buffer, 0, count);
			}

			// 关闭流
			writeFileStream.Close();
			readFileStream.Close();

			DateTime after = DateTime.Now;
			TimeSpan ts = after.Subtract(before);
			Console.WriteLine(ts);
		}
	}
}
